<template>
  <view class="login-container">
    <view class="login-box">
      <view class="title">用户登录</view>
      <view class="input-group">
        <input 
          class="input-item" 
          type="text" 
          v-model="loginForm.username" 
          placeholder="请输入用户名"
        />
        <input 
          class="input-item" 
          type="password" 
          v-model="loginForm.password" 
          placeholder="请输入密码"
        />
      </view>
      <button class="login-btn" @click="handleLogin">登录</button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      }
    }
  },
  methods: {
    handleLogin() {
      if (!this.loginForm.username || !this.loginForm.password) {
        uni.showToast({
          title: '请输入用户名和密码',
          icon: 'none'
        })
        return
      }
      
      // 调用登录接口
      uni.request({
        url: `${uni.$config.baseUrl}/userlogin/login`,
        method: 'POST',
        data: this.loginForm,
        success: (res) => {
          if (res.data.code === 200) {
            // 登录成功
            uni.showToast({
              title: '登录成功',
              icon: 'success'
            })
            // 存储用户信息
            uni.setStorageSync('userInfo', res.data.data)
            // 跳转到首页
            uni.reLaunch({
              url: '/pages/index/index'
            })
          } else {
            uni.showToast({
              title: res.data.msg || '登录失败',
              icon: 'none'
            })
          }
        },
        fail: () => {
          uni.showToast({
            title: '网络错误',
            icon: 'none'
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f5f5f5;
  
  .login-box {
    width: 80%;
    padding: 40rpx;
    background-color: #fff;
    border-radius: 20rpx;
    box-shadow: 0 0 20rpx rgba(0, 0, 0, 0.1);
    
    .title {
      font-size: 36rpx;
      font-weight: bold;
      text-align: center;
      margin-bottom: 40rpx;
      color: #333;
    }
    
    .input-group {
      margin-bottom: 40rpx;
      
      .input-item {
        width: 100%;
        height: 88rpx;
        border: 2rpx solid #ddd;
        border-radius: 8rpx;
        padding: 0 20rpx;
        margin-bottom: 20rpx;
        font-size: 28rpx;
        
        &:last-child {
          margin-bottom: 0;
        }
      }
    }
    
    .login-btn {
      width: 100%;
      height: 88rpx;
      line-height: 88rpx;
      background-color: #007AFF;
      color: #fff;
      font-size: 32rpx;
      border-radius: 8rpx;
      
      &:active {
        opacity: 0.8;
      }
    }
  }
}
</style> 